package com.hw.dagl.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hw.dagl.dao.impl.UserDaoImpl;
import com.hw.dagl.model.Frm_Department;
import com.hw.dagl.model.Sys_User;
import com.hw.dagl.model.Vehicle;
import com.hw.dagl.service.UserService;
import com.hw.dao.utils.EntityTools;
import com.hw.dao.utils.OraclePageDaoBase;
import com.hw.dao.utils.Page;
import com.hw.interceptor.SystemLog;
import com.hw.utils.EncryptUtil;
import com.hw.utils.IpUtil;



@Service
public class UserServiceImpl implements UserService{

	@Resource
	private OraclePageDaoBase oraclePageDaoBase;
	
	@Resource
	private UserDaoImpl userDaoImpl;

	/**
	 * 根据用户编号查询用户信息
	 */
	@Override
	@Transactional(readOnly=false,rollbackFor=Exception.class)
	public Sys_User findUserByYhdh(Sys_User user) {
		String sql=" select u.*,r.rname,r.ipks ipks_r,r.ipjs ipjs_r,r.jsyxq,d.bmmc,d.sjbm,r.shqx from SYS_USER u "
				+ " left join SYS_ROLE r on u.rid=r.rid "
				+ " left join FRM_DEPARTMENT_local d on u.glbm=d.glbm "
				+ " where u.yhdh= ? ";
		List<Sys_User> list = new ArrayList<Sys_User>();
		if(user != null){
			 list= oraclePageDaoBase.query(sql, new Object[]{user.getYhdh()},new RowMapper<Sys_User>(){
				private EntityTools<Sys_User> et = new EntityTools<Sys_User>();
				@Override
				public Sys_User mapRow(ResultSet rs, int index)
						throws SQLException {
					Sys_User yhxx = new Sys_User();
					et.setColumnsValueByResultSet(rs, yhxx);
					return yhxx;
				}
			});
		}
		return list.size() > 0? list.get(0) : null;
	}
	
	/**
	 * 分页查询用户信息
	 */
	@Override
	@SystemLog(Modelname="用户管理",operationType="3",operationName="查询用户列表")
	public Page<Sys_User> findUser(Page<Sys_User> pages,Sys_User info,String userSsbm) {
		StringBuffer sql=new StringBuffer(" select u.*,de.bmmc,r.rname from SYS_USER u "
				+ " left join FRM_DEPARTMENT_local de on u.glbm = de.glbm "
				+ " left join SYS_ROLE r on r.rid=u.rid "
				+ " where 1=1 ");
		List<Object> args = new ArrayList<Object>();
		if(StringUtils.isNotEmpty(info.getYhdh())){
			sql.append(" AND u.yhdh like ? ");
			args.add("%"+info.getYhdh()+"%");
		}
		if(StringUtils.isNotEmpty(info.getXm())){
			sql.append(" AND u.xm like ? ");
			args.add("%"+info.getXm()+"%");
		}
		if(StringUtils.isNotEmpty(info.getRid())){
			sql.append(" AND u.rid = ? ");
			args.add(info.getRid());
		}
		/*if(StringUtils.isNotEmpty(info.getGlbm())){
			sql.append(" AND u.glbm = ? ");
			args.add(info.getGlbm());
		}*/
		if(StringUtils.isNotEmpty(info.getZt())){
			sql.append(" AND u.zt = ? ");
			args.add(info.getZt());
		}
		sql.append(" and u.glbm in (select d.glbm from FRM_DEPARTMENT_local d start with d.glbm=? connect by d.sjbm=prior d.glbm) "
				+ " order by u.scdlsj desc ");
		args.add(userSsbm);
		return oraclePageDaoBase.queryForPage(sql.toString(), pages, new RowMapper<Sys_User>(){
			private EntityTools<Sys_User> et = new EntityTools<Sys_User>();
			@Override
			public Sys_User mapRow(ResultSet rs, int arg1)throws SQLException {
				Sys_User yhxx = new Sys_User();
				et.setColumnsValueByResultSet(rs, yhxx);
				return yhxx;
			}
		}, args.toArray());
	}
	/*
	 * 新增用户
	 */
	@Override
	@SystemLog(Modelname="用户管理",operationType="1",operationName="新增系统用户")
	@Transactional(readOnly=false,rollbackFor=Exception.class)
	public boolean saveUser(Sys_User info){
		info.setMm(EncryptUtil.encrypt(info.getMm().trim(), "MD5"));
		info.setScdlip(IpUtil.getIp());
		String sql=" insert into sys_user (yhdh, xm, mm, glbm, sfzmhm, ipks, ipjs, zhyxq, mmyxq, zt, zp, cjsj, scdlsj, scdlip, cjr, rid,GZDD) "
				+ " values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, sysdate, sysdate, ?, ?, ?,?) ";
		int flag=oraclePageDaoBase.update(sql, new Object[]{info.getYhdh(),info.getXm(),info.getMm(),info.getGlbm(),info.getSfzmhm(),
				   info.getIpks(),info.getIpjs(),info.getZhyxq(),info.getMmyxq(),info.getZt(),info.getZp(),
				   info.getScdlip(),info.getCjr(),info.getRid(),info.getGzdd()});
		return flag!=-1;
	}
	/*
	 * 批量删除用户
	 */
	@Override
	@SystemLog(Modelname="用户管理",operationType="4",operationName="删除系统用户")
	@Transactional(readOnly=false,rollbackFor=Exception.class)
	public boolean delUser(String ids){
		if(StringUtils.isNotEmpty(ids)){
			ids=ids.replaceAll(",", "','");
			String sql=" delete from  SYS_USER u where u.yhdh in ('"+ids+"') ";
			int flag=oraclePageDaoBase.update(sql);
			return flag!= -1;
		}else{
			return true;
		}
	}
	/*
	 * 修改用户信息
	 */
	@Override
	@SystemLog(Modelname="用户管理",operationType="2",operationName="修改系统用户")
	@Transactional(readOnly=false,rollbackFor=Exception.class)
	public boolean updateUser(Sys_User info){
		if(info.getMm().equals("111111")){//密码未改�?
			String sql=" update sys_user set xm = ?,glbm = ?,sfzmhm = ?,ipks = ?,ipjs = ?,zhyxq = ?, "
					+ " mmyxq = ?,zt = ?,zp = ?,scdlsj = ?,scdlip = ?,cjr = ?,rid = ?,GZDD=? where yhdh = ? ";
			int flag=oraclePageDaoBase.update(sql, new Object[]{info.getXm(),info.getGlbm(),info.getSfzmhm(),
					   info.getIpks(),info.getIpjs(),info.getZhyxq(),info.getMmyxq(),info.getZt(),info.getZp(),
					   info.getScdlsj(),info.getScdlip(),info.getCjr(),info.getRid(),info.getGzdd(),info.getYhdh()});
			return flag!=-1;
		}else{//密码改变
			info.setMm(EncryptUtil.encrypt(info.getMm().trim(), "MD5"));
			String sql=" update sys_user set xm = ?,mm = ?,glbm = ?,sfzmhm = ?,ipks = ?,ipjs = ?,zhyxq = ?, "
					+ " mmyxq = ?,zt = ?,zp = ?,scdlsj = ?,scdlip = ?,cjr = ?,rid = ? where yhdh = ? ";
			int flag=oraclePageDaoBase.update(sql, new Object[]{info.getXm(),info.getMm(),info.getGlbm(),info.getSfzmhm(),
					   info.getIpks(),info.getIpjs(),info.getZhyxq(),info.getMmyxq(),info.getZt(),info.getZp(),
					   info.getScdlsj(),info.getScdlip(),info.getCjr(),info.getRid(),info.getYhdh()});
			return flag!=-1;
		}
	}
	
	/*
	 * 修改用户上次登录信息
	 */
	@Override
	@Transactional(readOnly=false,rollbackFor=Exception.class)
	public boolean updateUserLogin(String yhdh,HttpServletRequest request){
		//String IP=IpUtil.getIp();
		String IP=null;
		try {
			IP = IpUtil.getIpAddr(request);
		} catch (Exception e) {
			e.printStackTrace();
		}
		String sql=" update sys_user set scdlsj = sysdate,scdlip = ? where yhdh = ? ";
		int flag=oraclePageDaoBase.update(sql, new Object[]{IP,yhdh});
		return flag!=-1;
	}
	/**
     * 查询部门信息
     * @return
    */
	@Override
	public List<Frm_Department> findBm(){
		String sql = " select de.glbm,de.bmmc,de.bmqc from FRM_DEPARTMENT de order by de.glbm asc ";
		List<Frm_Department> list = new ArrayList<Frm_Department>();
		list = oraclePageDaoBase.query(sql, new Object[]{}, new RowMapper<Frm_Department>(){
			EntityTools<Frm_Department> et = new EntityTools<Frm_Department>();
			@Override
			public Frm_Department mapRow(ResultSet rs, int index)
					throws SQLException {
				Frm_Department de = new Frm_Department();
				et.setColumnsValueByResultSet(rs, de);
				return de;
			}
		});
		return list.size() > 0 ? list : null;
	}

	@Override
	@Transactional(readOnly=false,rollbackFor=Exception.class)
	public boolean updatePasswd(Sys_User info) {
		String sql ="update SYS_USER set mm=? where yhdh=?";
		int i =oraclePageDaoBase.update(sql, new Object[]{info.getMm(),info.getYhdh()});
		return i==1 ? true : false;
	}

	@Override
	public Sys_User selectSsbmOfUser(Sys_User user) {
		String sql = "select t.fzjg,t.glbm,r.rid from FRM_DEPARTMENT_LOCAL t,SYS_USER r where t.glbm=r.glbm and r.yhdh='"+user.getYhdh()+"' ";
		List<Sys_User> list= oraclePageDaoBase.query(sql, new Object[]{}, new BeanPropertyRowMapper<Sys_User>(Sys_User.class));
		return list.size()>=1 ? list.get(0) : null;
	}
}
